import React from 'react';
import { Text, Block } from '@tarojs/components';

const Float: React.FC<{ value: any; size?: string; left?: string|number; style?: string }> = (props) => {
    let value: string;
    if (props.value == undefined || props.value == 'undefined') {
        value = '0';
    } else {
        value = (props.value + '');
    }
    const size = props.size || '0.7em'; // 小数点后文字大小 建议使用em
    const left = props.left || 0; // 距离.的间距
    let isFloat = false; // 是否带有小数点
    let after = ''; // 小数点前
    let before = '';  // 小数点后
    if (value.indexOf('.') != -1) {
        after = value.substr(0, value.indexOf('.'));
        before = value.substr(value.indexOf('.') + 1, value.length);
        isFloat = true;
    }
    return (
        <Block>
            {
                isFloat ? <Text className='component_float font-family-price' style={props.style}>
                    {after}.<Text className='component_float-small font-family-price' style={`position: relative; left: ${left}; font-size: ${size};`}>
                        {before}
                    </Text>
                </Text>:<Text className='component_float font-family-price' style={props.style}>
                  {value}
                </Text>
            }
        </Block>
    )
}
export default Float;
